<?php
namespace App\Admin\Controllers\Stocking;

use App\Admin\Actions\Stocking\ImportSetDate;
use App\Exporter\Stocking\SkuSetDateExporter;
use App\Jobs\Stocking\SetDate;
use App\Models\Basics\SysIArea;
use App\Models\Product\ProductClassify;
use App\Models\Stocking\StockingSetDate;
use App\Models\Stocking\StockingSku;
use Encore\Admin\Admin as RAdmin;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Facades\Admin;
//use Encore\Admin\Form;
use Encore\Admin\Grid;
use Illuminate\Support\Facades\Request;
class SetDateController extends AdminController{

    protected $title = '设置备货月份';

    protected function grid()
    {
        RAdmin::html($this->_html());
        RAdmin::script($this->_script());
        $grid = new Grid(new StockingSetDate());
        $grid->column('classify.title','品类');
        $grid->column('area.area','站点');
        $grid->column('classify.season','季节性')->using(ProductClassify::$_season);
        $grid->column('purchase_cycle','生产周期(天)');
        $grid->column('sea_day','海运时效(天)');
        $grid->column('sea_available','海运最快可售')->display(function ($value){
            return StockingSku::getSaleDate($value);
        });
        if(Admin::user()->can('stocking.set-date.edit')){
            $grid->column('set_date','设定日期')->view('stocking.set-date.set-date')->style('width:15%');
        }else{
            $grid->column('set_date','设定日期');
        }
        $grid->column('status','预警')->bool()->filter(StockingSetDate::$_status)->help('设定日期已低于海运+2月');
        if(Admin::user()->can('stocking.set-date.edit')){
            $grid->tools(function (Grid\Tools $tools) {
                $tools->append(new ImportSetDate());
            });
        }
        $grid->exporter(new SkuSetDateExporter());
        $grid->disableActions();
        $grid->disableCreateButton();
        $grid->disableRowSelector();
        $grid->filter(function($filter){
            $filter->column(1/3, function ($filter){
                $filter->equal('classify_id', '品类')->select(ProductClassify::options());
            });
            $filter->column(1/3, function ($filter){
                $filter->equal('area_id', '站点')->select(SysIArea::options());
            });
            $filter->column(1/3, function ($filter) {
                $filter->equal('classify.season', '季节性')->select(ProductClassify::$_season);
            });
        });
        return $grid;
    }

    protected function _html(){
        return <<<HTML
<template id="editable-input-tpl">
    <div class="control-group form-group">
        <div class="editable-input" style="width: 80px;">
            <input id="pk-__pk__" type="text" class="form-control input-sm" placeholder="设定日期">
        </div>
        <div class="editable-buttons">
            <button id="submit-__pk__" type="button" style="margin-top: 2px;padding: 3px 6px;" class="btn btn-primary btn-xs editable-submit"><i class="glyphicon glyphicon-ok"></i></button>
            <button id="cancel-__pk__" type="button" style="margin-left: 0;margin-top: 2px;padding: 3px 6px;" class="btn btn-default btn-xs editable-cancel"><i class="glyphicon glyphicon-remove"></i></button>
        </div>
    </div>
</template>
<template id="editable-a-tpl">
    <a href="javascript:void(0);" class="grid-editable-set_date" style="padding-top: 5px;display: inline-block;" data-pk="__pk__" data-url="__url__" data-value="__value__">__val__</a>
</template>
<template id="editable-loading-tpl">
    <div class="editableform-loading"></div>
</template>
HTML;
    }

    protected function _script()
    {
        $token = csrf_token();
        $i = '<i class=\"fa fa-pencil\"></i>';
        return <<<EOT
$(document).on('click', '.grid-editable-set_date',function(e) {
    var a = $(this);
    var pk = a.data('pk');
    var url = a.data('url');
    var oValue = a.data('value');
    var val = a.text();
    var td = a.parent();
    td.empty();
    td.append($('#editable-input-tpl').html().replace(/__pk__/g, pk));
    var input = $('#pk-'+pk);
    input.val(oValue);
    input.datetimepicker({
        "format":"YYYY-MM-01",
        "locale":"zh-CN",
        "allowInputToggle":true
    });
    var value = oValue;
    var submit = $('#submit-'+pk);
    submit.on('click',function (e) {
        value = input.val().trim();
        val = value == '' ? "{$i}" : value;
        if(oValue != value){
            $.ajax({
                method: 'post',
                url: url,
                data: {
                    name:'set_date',
                    value:value,
                    pk:pk,
                    _method:'PUT',
                    _token:'{$token}',
                },
                beforeSend: function(XMLHttpRequest) {
                    td.empty();
                    td.append($('#editable-loading-tpl').html());
                },
                success: function (data) {
                    td.empty();
                    td.append($('#editable-a-tpl').html().replace(/__pk__/g, pk).replace(/__url__/g, url).replace(/__value__/g, value).replace(/__val__/g, val));
                    $.pjax.reload("#pjax-container");
                }
            });
        }else {
            td.empty();
            td.append($('#editable-a-tpl').html().replace(/__pk__/g, pk).replace(/__url__/g, url).replace(/__value__/g, value).replace(/__val__/g, val));
        }
    });
    var cancel = $('#cancel-'+pk);
    cancel.on('click',function (e) {
        val = oValue == '' ? "{$i}" : oValue ;
        td.empty();
        td.append($('#editable-a-tpl').html().replace(/__pk__/g, pk).replace(/__url__/g, url).replace(/__value__/g, value).replace(/__val__/g, val));
    });
});
EOT;
    }

    public function update($id)
    {
        $value = Request::get('value');
        $time = strtotime($value);
        if(!$time){
            return response()->json(['status' => false, 'message' => '操作失败']);
        }
        $re = StockingSetDate::query()->find($id);
        $re->set_date = date('Y-m-01',$time); //备货日期
        $re->status = StockingSetDate::setDateLe2Date($re->set_date,$re->sea_available); //预警状态
        $flag = $re->save();
        if($flag){
            //回写备货日期
            SetDate::dispatch($re->area_id,$re->classify_id,$re->set_date);
        }
        return response()->json(['status' => true, 'message' => '操作成功！']);
    }

}
